<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  .weui-cells__title {
    text-align: left;
  }
  .oitem {
    padding: 3px;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label {
    text-align: left;
  }
</style>
<template>
  <div class="addsell">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      添加收货地址
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form action="" method="post" id="formid">
        <input type="hidden" name="province" />
        <input type="hidden" name="city" />
        <input type="hidden" name="district" />
        <input type="hidden" name="customerid" :value="customerguid" />
        <div class="oitem">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收货城市</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="endCity" name="" placeholder="请选择收货城市" >
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">详细地址</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="addres" name="addres" placeholder="请输入收货详细地址">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收货联系人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="contact" name="contact" placeholder="请输入收货联系人">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="contactno" name="contactno" placeholder="请输入收货人电话">
            </div>
          </div>
        </div>

        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary halfbtn subbtn" href="javascript:" @click="postForm">保存</a>
          <a class="weui-btn weui-btn_primary halfbtn" href="javascript:;" @click="$router.back(-1)">返回</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AddressList2',
  data () {
    return {
      openid: localStorage.openid,//'oIuV2v77MaLCAQILhjI6h6oTWpHI',
      customerguid: ''
  }
  },
  created() {
    window.addEventListener('setItem', ()=> {
      this.openid = localStorage.getItem('openid');
    })
  },
  mounted () {
    this.customerguid = this.$store.state.customer.guid;
    console.log('customerguid:', this.customerguid)

    $("#endCity").cityPicker({
      title: "请选择收货地址",
      onChange: function (data) {
        let addr = data.displayValue;
        $('input[name="province"]').val(addr[0]);
        $('input[name="city"]').val(addr[1]);
        $('input[name="district"]').val(addr[2]);
      }
    });
  },
  methods: {
    //提交表单数据
    postForm: function(type) {
      let that = this;

      if ($('input[name="city"]').val() == '') {
        $.alert('请选择收货城市！');
        return false;
      } else if ($('input[name="address"]').val() == '') {
        $.alert('请输入详细地址！');
        return false;
      } else if ($('input[name="contact"]').val() == '') {
        $.alert('请输入联系人！');
        return false;
      } else if ($('input[name="contactno"]').val() == '') {
        $.alert('请输入联系人电话！');
        return false;
      }
      //防止重复提交
      $('.subbtn').attr("disabled",true).css("pointer-events","none");
        //显示提交状态
        $.showLoading("正在提交");

      this.axios({
        method: 'post',
        url: '/prod-api/customer/addresrecord',
        data: this.serialize2Obj($("#formid").serializeArray())
      }).then(response => {
        //恢复提交按钮可点击
        $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();

        if (response.data.code == 200) {
            this.toastSuccess("保存成功");
            this.$router.push({name: 'AddressList2', query: {customerguid: this.customerguid}});
        } else if (response.data.msg) {
            this.toastText(response.data.msg);
        } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
          this.toastError('系统错误');
        }
      }).catch(function (error) {
        //恢复提交按钮可点击
        $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();
        that.toastError('系统错误');
      });
    },
  }
}
</script>
